package com.org.daniel.service.util;

import com.org.daniel.common.constant.enumerators.RedisChannelEnum;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author Daniel
 */
@Service
public class RedisManagerService {
	private static Logger logger = LoggerFactory.getLogger(RedisManagerService.class);

	@Resource(name = "redisTemplateMaster")
	private RedisTemplate redisTemplateMaster;

	@Resource(name = "redisTemplateSecond")
	private RedisTemplate redisTemplateSecond;

	/**
	 * 获取redis对应key的value值
	 * @param redisChannel database
	 * @param key key
	 * @return object
	 */
	public Object getValue(String redisChannel, String key){
		Object ret = null;
		RedisTemplate redisTemplate = switchRedisTemp(redisChannel);
		if (redisTemplate == null){
			logger.info("redisTemplate is null, channel={}", redisChannel);
			return null;
		}
		if (StringUtils.isBlank(key)){
			logger.info("redis key is blank");
			return null;
		}
		try {
			if (!redisTemplate.hasKey(key)){
				logger.info("redis key not exist");
				return null;
			}
			BoundValueOperations valueOperations = redisTemplate.boundValueOps(key);
			ret = valueOperations.get();
			logger.info("get from redis {}:{}", key, ret);
		} catch (Exception e){
			logger.error("get redis value error ", e);
		}
		return ret;
	}

	/**
	 * 设置redis的值
	 * @param redisChannel database
	 * @param key key
	 * @param timeout 超时设置s
	 */
	public void setValue(String redisChannel, String key, String value, long timeout, TimeUnit timeUnit){
		RedisTemplate redisTemplate = switchRedisTemp(redisChannel);
		if (redisTemplate == null){
			logger.info("redisTemplate is null, channel={}", redisChannel);
		}
		if (StringUtils.isBlank(key)){
			logger.info("redis key is blank");
		}
		try {
			BoundValueOperations valueOperations = redisTemplate.boundValueOps(key);
			valueOperations.set(value, timeout, timeUnit);
			logger.info("set to redis {}:{}", key, value);
		} catch (Exception e){
			logger.error("set redis value error ", e);
		}
	}

	/**
	 * 删除key
	 * @param redisChannel database
	 * @param key key
	 */
	public void delKey(String redisChannel, String key){
		RedisTemplate redisTemplate = switchRedisTemp(redisChannel);
		if (redisTemplate == null){
			logger.info("redisTemplate is null, channel={}", redisChannel);
		}
		try {
			redisTemplate.delete(key);
			logger.info("del from redis {}", key);
		} catch (Exception e){
			logger.error("del redis key error ", e);
		}
	}

	/**
	 * 根据数据源获取不同的redisTemplate对象
	 * @param redisChannel redisChannel
	 * @return RedisTemplate
	 */
	private RedisTemplate switchRedisTemp(String redisChannel){
		if (RedisChannelEnum.SECOND.getCode().equals(redisChannel)){
			return redisTemplateSecond;
		}
		return redisTemplateMaster;
	}

}
